﻿Public Class WfCadastroOS
    Inherits System.Web.UI.Page
    Dim despLogado As New Despachante

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        despLogado = Session("DespLogado")
        If IsPostBack = False Then
            Dim StatusDao As New Status_osDAO
            Dim lista As New List(Of Status_Os)

            lista = StatusDao.ListaTodos
            For Each sta As Status_Os In lista
                DropStatus.Items.Add(sta.DescStat)
            Next
            populasServiço()
        End If


    End Sub

    Public Sub Populatodos()
        Dim clienteDao As New ClienteDAO
        GvClientes.AutoGenerateColumns = False
        GvClientes.DataSource = clienteDao.clienteJoinTodos(despLogado)
        GvClientes.DataBind()
    End Sub


    Protected Sub DropTipoPesq_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropTipoPesq.SelectedIndexChanged
        If DropTipoPesq.SelectedValue = "Todos" Then
            tbBusca.Enabled = False
        Else
            tbBusca.Enabled = True
        End If
    End Sub

    Protected Sub BuscarCli_Click(sender As Object, e As EventArgs) Handles BuscarCli.Click
        Dim objCliDao As New ClienteDAO
        Dim objCli As New Cliente
        Dim dados As New DataTable



        lbmsg.Text = ""
        GvClientes.DataBind()

        If DropTipoPesq.SelectedValue = "Nome" Then

            Try
                If tbBusca.Text <> "" Then
                    objCli.NomeCli = tbBusca.Text
                    GvClientes.AutoGenerateColumns = False
                    dados = objCliDao.clienteJoinNome(objCli, despLogado)
                    If dados.Rows.Count <> 0 Then
                        GvClientes.DataSource = dados
                        GvClientes.DataBind()
                    Else
                        lbmsg.ForeColor = Drawing.Color.Red
                        lbmsg.Text = "Nenhum resultado encontrado !"

                    End If
                Else
                    lbmsg.ForeColor = Drawing.Color.Red
                    lbmsg.Text = "Digite um Nome !"
                End If
            Catch ex As Exception
                lbmsg.ForeColor = Drawing.Color.Red
                lbmsg.Text = "Digite apenas Letras !"
            End Try

        End If

        If DropTipoPesq.SelectedValue = "Código" Then

            Try
                If tbBusca.Text <> "" Then
                    objCli.CodCli = tbBusca.Text
                    GvClientes.AutoGenerateColumns = False
                    dados = objCliDao.clienteJoinCodigo(objCli, despLogado)
                    If dados.Rows.Count <> 0 Then
                        GvClientes.DataSource = dados
                        GvClientes.DataBind()
                    Else
                        lbmsg.ForeColor = Drawing.Color.Red
                        lbmsg.Text = "Nenhum resultado encontrado !"

                    End If
                Else
                    lbmsg.ForeColor = Drawing.Color.Red
                    lbmsg.Text = "Digite um Código !"
                End If
            Catch ex As Exception
                lbmsg.ForeColor = Drawing.Color.Red
                lbmsg.Text = "Digite apenas números !"
            End Try

        End If

        If DropTipoPesq.SelectedValue = "Cpf/Cnpj" Then

            Try
                If tbBusca.Text <> "" Then
                    objCli.CpfCnpjCli = tbBusca.Text
                    GvClientes.AutoGenerateColumns = False
                    dados = objCliDao.clienteJoinCpf(objCli, despLogado)
                    If dados.Rows.Count <> 0 Then
                        GvClientes.DataSource = dados
                        GvClientes.DataBind()
                    Else
                        lbmsg.ForeColor = Drawing.Color.Red
                        lbmsg.Text = "Nenhum resultado encontrado !"
                    End If
                Else
                    lbmsg.ForeColor = Drawing.Color.Red
                    lbmsg.Text = "Digite um Cpf ou Cnpj !"
                End If
            Catch ex As Exception
            End Try

        End If

        If DropTipoPesq.SelectedValue = "Todos" Then
            Populatodos()
        End If

    End Sub

    Private Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
        If Session("DespLogado") Is Nothing Then
            Response.Redirect("/Screens/WF1.aspx")
        End If
    End Sub

    Protected Sub GvClientes_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GvClientes.PageIndexChanging
        GvClientes.PageIndex = e.NewPageIndex
        Populatodos()
    End Sub

    Protected Sub GvClientes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GvClientes.SelectedIndexChanged
        TbCodigoCli.Text = GvClientes.SelectedRow.Cells(1).Text
        tbNomeCli.Text = GvClientes.SelectedRow.Cells(2).Text
        TbData.Text = System.DateTime.Now
    End Sub


    Protected Sub GerarOs_Click(sender As Object, e As EventArgs) Handles GerarOs.Click
        Try
            Dim objOs As New Os
            Dim objOsDao As New OsDAO
            Dim objCliDao As New ClienteDAO
            Dim objservDao As New ServicoDAO
            Dim objserv As New Servico
            Dim objStaDao As New Status_osDAO
            Dim objSta As New Status_Os

            objSta.DescStat = DropStatus.SelectedItem.Text

            objOs.Cliente = objCliDao.SelectCliId(TbCodigoCli.Text)
            objOs.DataOs = TbData.Text
            objOs.Despachante = despLogado
            objOs.ObsOs = tbOs.Text
            objOs.PgtOs = DropPag.SelectedValue.ToString
            objOs.Servico = objservDao.SelectServId(GvServicos.SelectedRow.Cells(1).Text)
            objOs.Status = objStaDao.selectStatusDesc(objSta)
            objOs.ValorOs = tbvalor.Text
            objOsDao.Inserir(objOs)
            lbcad.ForeColor = Drawing.Color.Blue
            lbcad.Text = " Ordem de Serviço Cadastrada com Sucesso !"

        Catch ex As Exception
            lbcad.ForeColor = Drawing.Color.Red
            lbcad.Text = " Erro ao Cadastrar Ordem de Serviço !"
        End Try
    End Sub

    Protected Sub GvServicos_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GvServicos.SelectedIndexChanged
        tbvalor.Text = GvServicos.SelectedRow.Cells(3).Text
    End Sub

    Public Sub populasServiço()
        Dim servicoDao As New ServicoDAO
        GvServicos.AutoGenerateColumns = False
        GvServicos.DataSource = servicoDao.ListaTodos
        GvServicos.DataBind()
    End Sub

End Class